﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DrugStorehouseMIS.BLL;
using DrugStorehouseMIS.Comm;

namespace DrugStorehouseMIS.StockInAndOut
{
    public partial class frmStockIn : Form
    {
        private DataTable dtSupplier, dtDrug;

        private StockInHeader stockInHeaderBLL;
        private StockInDetail stockInDetailBLL;

        private StockInType stockInTypeBLL;
        private Supplier supplierBLL;
        private Drug drugBLL;
        private MeasuringUnit measuringUnitBLL;

        public frmStockIn()
        {
            InitializeComponent();
            stockInHeaderBLL = new StockInHeader();
            stockInDetailBLL = new StockInDetail();

            stockInTypeBLL = new StockInType();
            supplierBLL = new Supplier();
            measuringUnitBLL = new MeasuringUnit();
            drugBLL = new Drug();

            dtSupplier = new DataTable();
            dtDrug = new DataTable();

            stockInHeaderBLL.SupplierName = new Supplier();
            stockInHeaderBLL.StockInTypeName = new StockInType();
        }

        private void frmStockIn_Load(object sender, EventArgs e)
        {
            txtStockInNum.Text = Tool.GenerateNum("RK"); //自动生成入库单号
            BindStockInType(); 
            BindMeasuringUnit();
            BindSupplier();
           
        }

        /// <summary>
        /// 绑定入库类型
        /// </summary>
        public void BindStockInType()
        {
            IList<StockInType> list = stockInTypeBLL.GetAllStockInType();
            foreach (StockInType obj in list)
            {
                cbxStockInType.Items.Add(obj.StockInTypeName);
            }
            cbxStockInType.SelectedIndex = 0;
        }

        /// <summary>
        /// 绑定计量单位
        /// </summary>
        public void BindMeasuringUnit()
        {
            IList<MeasuringUnit> list = measuringUnitBLL.GetAllMeasuringUnit();
            foreach (MeasuringUnit obj in list)
            {
                cbxMeasuringUnit.Items.Add(obj.MeasuringUnitName);
            }
            cbxMeasuringUnit.SelectedIndex = 0;
        }

        public void BindDrug()
        {
            dgvDrug.DataSource = dtDrug = drugBLL.GetAllDrug();
        }

        /// <summary>
        /// 绑定供应单位 
        /// </summary>
        public void BindSupplier()
        {
            dgvSupplier.DataSource = dtSupplier = supplierBLL.GetAllSupplier();
        }

        /// <summary>
        /// 绑定入库单药品信息
        /// </summary>
        public void LoadStockInDrugInfo()
        {
            dgvDrugInfo.DataSource = stockInDetailBLL.GetStockInDeatilByNum(txtStockInNum.Text.Trim());
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtDrugCode.Text = "";
            txtAmount.Text = "";
            txtBatchNum.Text = "";
            txtDrugName.Text = "";
            txtImportPrice.Text = "";
            txtQuantity.Text = "";
            txtRetailPrice.Text = "";
            txtSpecifications.Text = "";
            txtSupplier.Text = "";
            txtTradePrice.Text = "";
            rtbRemark.Text = "";
            this.dgvSupplier.Visible = false;
            this.dgvDrug.Visible = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public bool Check()
        {
            if (txtDrugCode.Text.Trim() != "" && txtSupplier.Text.Trim() != "" && txtTradePrice.Text.Trim() != "" && txtRetailPrice.Text.Trim() != "" && txtImportPrice.Text.Trim()!="" && txtQuantity.Text.Trim() != "" && txtAmount.Text.Trim() != "")
                return btnOK.Enabled=btnAdd.Enabled= true;
            else
                return btnOK.Enabled = btnAdd.Enabled = false;
        }

        /// <summary>
        /// 根据输入的数量和进价来计算金额
        /// </summary>
        public void Calcluate()
        {
            float price, amount;
            long qty ;

            if (txtImportPrice.Text.Trim() != "")
            {
                price = Convert.ToSingle(txtImportPrice.Text.Trim());
            }
            else
            {
                price = 0;
            }
            if (txtQuantity.Text.Trim() != "")
            {
                qty = Convert.ToInt64(txtQuantity.Text.Trim());
            }
            else
            {
                qty = 0;
            }
            amount = price * qty;
            txtAmount.Text = Convert.ToString(amount);
        }

        private void txtTradePrice_TextChanged(object sender, EventArgs e)
        {
            Tool.OnlyNumber(txtTradePrice,'.');
            this.Check();
        }

        private void txtRetailPrice_TextChanged(object sender, EventArgs e)
        {
            Tool.OnlyNumber(txtRetailPrice, '.');
            this.Check();
        }

        private void txtImportPrice_TextChanged(object sender, EventArgs e)
        {
            Tool.OnlyNumber(txtImportPrice, '.');
            this.Check();

            this.Calcluate();
        }

        private void txtQuantity_TextChanged(object sender, EventArgs e)
        {
            Tool.OnlyNumber(txtQuantity, '0');
            this.Check();

            this.Calcluate();
        }

        #region 药品的智能录入
        private void txtDrugCode_TextChanged(object sender, EventArgs e)
        {
            this.Check();

            int times = 0;

            this.dgvDrug.Top = this.txtDrugCode.Top + this.txtDrugCode.Height;

            if (times == 0)
            {
                times = 1;
                BindDrug();
            }

            if (txtDrugCode.Text == "")
            {
                if (dtDrug.DefaultView.Count != 0)
                    dgvDrug.Visible = true;
            }

            dtDrug.DefaultView.RowFilter = "drug_code like '" + txtDrugCode.Text.Trim() + "%'";

            if (dtDrug.DefaultView.Count != 0)
            {
                dgvDrug.Visible = true;
                dgvDrug.CurrentCell = dgvDrug[0, 0];
            }
            else
                dgvDrug.Visible = false;
        }

        private void txtDrugCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (dgvDrug.Rows.Count == 0)
                    return;

                dgvDrug.Focus();
                if (dgvDrug.CurrentRow.Index + 1 < dgvDrug.Rows.Count)
                {
                    dgvDrug.CurrentCell = dgvDrug[0, dgvDrug.CurrentRow.Index + 1];
                }
            }

            if (e.KeyCode == Keys.Enter)
            {
                //txtDrugName.Text = dgvDrug.CurrentRow.Cells[2].Value.ToString(); 
                txtDrugCode.Text = dgvDrug.CurrentRow.Cells[0].Value.ToString();       //录入药品代号
                //txtSpecifications.Text = dgvDrug.CurrentRow.Cells[6].Value.ToString(); //录入药品规格
                //txtDrugName.Text = dgvDrug.CurrentRow.Cells[2].Value.ToString();       //录入药品名
                dgvDrug.Visible = false;

            }
        }

        private void dgvDrug_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtDrugCode.Text = dgvDrug.CurrentRow.Cells[0].Value.ToString();
                dgvDrug.Visible = false;

            }
        }


        #endregion

        #region  供应单位的智能录入
        private void txtSupplier_TextChanged(object sender, EventArgs e)
        {
            this.Check();

            int times = 0;

            this.dgvSupplier.Top = this.txtSupplier.Top + this.txtSupplier.Height;
           
            if (times == 0)
            {
                times = 1;
                BindSupplier();
            }

            if (txtSupplier.Text == "")
            {
                if (dtSupplier.DefaultView.Count!=0)
                    dgvSupplier.Visible = true;
            }

            dtSupplier.DefaultView.RowFilter = "supplier_name like '" + txtSupplier.Text.Trim() + "%'";

            if (dtSupplier.DefaultView.Count != 0)
            {
                dgvSupplier.Visible = true;
                dgvSupplier.CurrentCell = dgvSupplier[0, 0];
            }
            else
                dgvSupplier.Visible = false;
           
        }

        private void txtSupplier_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (dgvSupplier.Rows.Count == 0)
                    return;

                dgvSupplier.Focus();
                if (dgvSupplier.CurrentRow.Index + 1 < dgvSupplier.Rows.Count)
                {
                    dgvSupplier.CurrentCell = dgvSupplier[0, dgvSupplier.CurrentRow.Index + 1];
                }
            }

            if (e.KeyCode == Keys.Enter)
            {
                txtSupplier.Text = dgvSupplier.CurrentRow.Cells[1].Value.ToString();
                dgvSupplier.Visible = false;

            }
        }

        private void dgvSupplier_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtSupplier.Text = dgvSupplier.CurrentRow.Cells[1].Value.ToString();
                dgvSupplier.Visible = false;

            }
        }

        #endregion

        private void btnAdd_Click(object sender, EventArgs e)
        {
            StockInDetail detail = new StockInDetail();
            detail.StockInNum = txtStockInNum.Text.Trim();
            detail.DrugNum = drugBLL.GetDrug(Convert.ToInt32(txtDrugCode.Text.Trim()));
            detail.Specifications = txtSpecifications.Text.Trim();
            detail.MeasuringUnit = cbxMeasuringUnit.Text;
            detail.ProductedArea = txtProductArea.Text.Trim();
            detail.ProductedDatetime = dtpProductedDate.Value;
            detail.ExpiryDatetime = dtpExpiryDate.Value;
            detail.ImportPrice = Convert.ToSingle(txtImportPrice.Text.Trim());
            detail.Quantity = Convert.ToInt32(txtImportPrice.Text.Trim());
            detail.RetailPrice = Convert.ToSingle(txtRetailPrice.Text.Trim());
            detail.TradePrice = Convert.ToSingle(txtTradePrice.Text.Trim());
            detail.Amount = Convert.ToSingle(txtAmount.Text.Trim());
            detail.BatchNum = txtBatchNum.Text.Trim();

            stockInDetailBLL.CreateStockInDetail(detail);

            LoadStockInDrugInfo();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            StockInHeader header = new StockInHeader();
            header.StockInNum = txtStockInNum.Text.Trim();
            header.StockInTypeNum = stockInTypeBLL.GetStockInType(cbxStockInType.Text);
            header.StockInDatetime = dtpStockInDate.Value;
            header.SupplierNum = supplierBLL.GetSupplier(txtSupplier.Text.Trim());
            header.CreatedBy = "";
            header.ApprovalBy = "";
            header.Remark = rtbRemark.Text;
            header.Status = "未审核";

            stockInHeaderBLL.CreateStockInHeader(header);
            this.Close();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadStockInDrugInfo();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

        }    
    }
}